package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.system.service.MsAdminUserRoleService;
import ipebg.mzk.provider.system.dao.MsAdminUserRoleDao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("msAdminUserRoleServiceImpl")
public class MsAdminUserRoleServiceImpl  implements MsAdminUserRoleService {
	private static Log log = LogFactory.getLog(MsAdminUserRoleServiceImpl.class);
	@Resource(name="msAdminUserRoleDao")
	MsAdminUserRoleDao msAdminUserRoleDao;
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveUserRole(MsAdminUserRole userRole){
		try{
			msAdminUserRoleDao.saveOrUpdate(userRole);
			//System.out.println("Insert new User RolE Ref: UserAdminId:"+userRole.getAdminId()+"   Role Id:"+userRole.getRoleId());
			log.info("Insert new User RolE Ref: UserAdminId:"+userRole.getAdminId()+"   Role Id:"+userRole.getRoleId());
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public MsAdminUserRole findMsUserRoleByAdminIdAndRoleId(String adminId, String roleId) throws Exception {
		MsAdminUserRole UR=null;
		try{
			List<MsAdminUserRole> list = msAdminUserRoleDao.queryForList("from MsAdminUserRole ur where ur.adminId=? and ur.roleId=?", new String[]{adminId,roleId});
			if(list!=null&&list.size()>0){
				UR=list.get(0);
			}
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return UR;
	}
}
